<template>
  <div id="article">
    <div>
      <h2 class="h2">
        <i class="el-icon-s-order"></i>
        新闻资讯
      </h2>
    </div>
    <div class>
      <el-button
        size="small"
        type="primary"
        icon="el-icon-circle-plus"
        @click="toAdd"
        >新增</el-button
      >
      <div class="widthDiv marginLeft">
        <el-input
          size="small"
          clearable
          v-model="pageParam.title"
          placeholder="请输入内容"
        ></el-input>
      </div>
      <div class="widthDiv">
        <el-select
          size="small"
          clearable
          v-model="pageParam.categoryId"
          placeholder="请选择分类"
        >
          <el-option
            v-for="item in categoryData"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          ></el-option>
        </el-select>
      </div>
      <el-button
        size="small"
        type="primary"
        icon="el-icon-search"
        @click="toSearch"
        >搜索</el-button
      >
    </div>

    <el-table :data="tableData">
      <el-table-column label="发布时间" width="200">
        <template slot-scope="scope">
          <i class="el-icon-time"></i>
          <span style="margin-left: 10px">{{
            moment(scope.row.publishTime).format("YYYY-MM-DD HH:mm:ss")
          }}</span>
        </template>
      </el-table-column>
      <el-table-column label="标题" width="600">
        <template slot-scope="scope">
          <el-tag size="medium" type="success" effect="dark">{{
            scope.row.title
          }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="status" label="状态" width="100">
      </el-table-column>
      <el-table-column
        prop="category.name"
        label="所属分类"
        width="120"
      ></el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button
            icon="el-icon-edit"
            size="mini"
            type="primary"
            @click="handleEdit(scope.row)"
            >编辑</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <div>
      <el-pagination
        background
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="pageParam.page"
        :page-sizes="[5, 10, 15, 20]"
        :page-size="pageParam.pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </div>
    <!-- 第二个div结尾！ -->

    <el-dialog
      :title="title"
      :visible.sync="visible"
      :before-close="beforeClose"
      width="70%"
    >
      <el-form :model="form" :rules="rules" ref="ruleForm">
        <!-- 标题！ -->
        <el-form-item label="标题" :label-width="formLabelWidth" prop="title">
          <el-input v-model="form.title" autocomplete="off"></el-input>
        </el-form-item>
        <!-- 所属分类！ -->
        <el-form-item
          label="所属分类"
          :label-width="formLabelWidth"
          prop="categoryId"
        >
          <el-select
            size="small"
            clearable
            v-model="form.categoryId"
            placeholder="请选择分类"
          >
            <el-option
              v-for="item in categoryData"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <!-- 资讯！ -->
        <el-form-item label="资讯" :label-width="formLabelWidth" prop="content">
          <tinymce v-model="form.content"></tinymce>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="toCancel">取 消</el-button>
        <el-button type="primary" @click="toSave('ruleForm')">确 定</el-button>
      </div>
    </el-dialog>
  </div>
  <!--第一个div结尾！  -->

  <!-- tem结尾了！ -->
</template>

<script>
//引入tinymce组件
import Tinymce from "../../components/Tinymce/index";
import axios from "../../utils/request";
let moment = require("moment");
export default {
  //注册组件
  components: {
    tinymce: Tinymce,
  },
  data() {
    return {
      formLabelWidth: "100px",
      //表单验证
      rules: {
        title: [{ required: true, message: "请输入资讯标题", trigger: "blur" }],
        categoryId: [
          { required: true, message: "请选择资讯分类", trigger: "blur" },
        ],
        content: [
          { required: true, message: "请输入资讯内容", trigger: "blur" },
        ],
      },
      form: {},
      title: "发布新闻资讯",
      visible: false,
      categoryData: [],
      total: 0,
      tableData: [],
      //分页查询的页数
      pageParam: {
        page: 1,
        //数据类型要求数字 打成字符串
        pageSize: 10,
        title: "",
        categoryId: "",
      },
    };
  },
  computed: {},
  methods: {
    moment,
    //编辑
    handleEdit(row) {
      this.title = "编辑分类信息";
      this.form = { ...row };
      this.visible = true;
    },
    // 点击确定按钮
    toSave(formName) {
      this.$refs[formName].validate((valid) => {
        // 校验通过
        if (valid) {
          console.log(this.form, "++__++");
          axios.post("/article/saveOrUpdate ", this.form).then((res) => {
            // console.log(res);
            if (res.status == 200) {
              //保存成功，关闭模态框，刷新数据
              this.$notify({
                title: "成功！",
                message: "保存成功",
                type: "success",
              });
              this.toCancel();
              this.pageQuery();
            } else {
              this.$notify.error({
                title: "失败",
                message: res.statusText,
              });
            }
          });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    // 关闭模态框
    toCancel() {
      this.$refs["ruleForm"].resetFields();
      this.visible = false;
    },
    //模态框关闭之前
    beforeClose(done) {
      this.$refs["ruleForm"].resetFields();
      done();
    },
    //添加按钮
    toAdd() {
      this.form = {};
      this.visible = true;
      this.title = "发布新闻资讯";
    },
    //搜索
    toSearch() {
      this.pageParam.page = 1;
      this.pageQuery();
    },
    handleSizeChange(val) {
      //   console.log(`每页 ${val} 条`);
      this.pageParam.pageSize = val;
      this.pageQuery();
    },
    handleCurrentChange(val) {
      //   console.log(`当前页: ${val}`);
      this.pageParam.page = val;
      this.pageQuery();
    },
    //分页查找
    async pageQuery() {
      let res = await axios.get("/article/pageQuery", {
        params: this.pageParam,
        // 少打个s params
      });
      this.tableData = res.data.list;
      this.total = res.data.total;
    },
    //查找分类
    async findAllCategory() {
      let res = await axios.get("/category/findAll");
      this.categoryData = res.data;
    },
  },
  created() {
    this.pageQuery();
    this.findAllCategory();
  },
  mounted() {},
};
</script>
<style scoped>
.h2 {
  margin-top: 0;
}
.widthDiv {
  width: 200px;
  display: inline-block;
  margin-left: 10px;
  margin-bottom: 10px;
  /* margin-top: 10px; */
}
</style>